package com.bijiawang.user.controller;
import com.bijiawang.user.bean.Userinfo;
import com.bijiawang.user.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

@Controller

public class RegisterController {

    @Autowired
    private UserinfoService userinfoService;

    @PostMapping("/handle_form")
    public String register(@RequestParam("username")String username,@RequestParam("phonenumber")String phonenumber,
                           @RequestParam("passw")String passw,@RequestParam("email")String email) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String hashpassw = encoder.encode(passw);
        Userinfo userinfo = new Userinfo(username, hashpassw, phonenumber, email);
        userinfoService.addUser(userinfo);
        return "login";
    }

    @GetMapping("/checkUsername/{username}")
    @ResponseBody
    public String checkusername(@PathVariable("username") String user){
        String s = userinfoService.returnUsername(user);
        if (s == null){
            return "true";
        }else{
            return "false";
        }

    }

    @GetMapping("/checkPhonename/{phonenumber}")
    @ResponseBody
    public String checkphonenumber(@PathVariable("phonenumber") String phonenumber){
        String p = userinfoService.returnPhonenumber(phonenumber);
        if (p == null){
            return "true";
        }else{
            return "false";
        }
    }

    @GetMapping("/checkEmail/{email}")
    @ResponseBody
    public String checkEmail(@PathVariable("email")String email){
        String s = userinfoService.returnEmail(email);
        if (s == null){
            return "true";
        }else {
            return "false";
        }
    }
}
